package com.bosssoft.trainee.nontax.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.trainee.nontax.entity.dto.UserDTO;
import com.bosssoft.trainee.nontax.entity.po.Menu;
import com.bosssoft.trainee.nontax.entity.po.User;
import com.bosssoft.trainee.nontax.entity.vo.MenuVO;
import com.bosssoft.trainee.nontax.entity.vo.UserQueryVO;
import com.bosssoft.trainee.nontax.mapper.MenuMapper;
import com.bosssoft.trainee.nontax.mapper.UserMapper;
import com.bosssoft.trainee.nontax.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqiang
 * @date 2022/6/16 21:16
 */
@Service
public class UserServiceImp extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;

    /**
     * 根据account查找user
     *
     * @param user 用户
     * @return 用户
     */
    public User findByUserAccount(User user) {
        return userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserAccount, user.getUserAccount()));
    }

    /**
     * 根据id查找user
     *
     * @param userId 用户id
     * @return 对应的用户
     */
    public User findUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 添加用户
     */
    public void saveUser(UserDTO userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        userMapper.insert(user);
    }

    /**
     * 获取用户对于菜单列表
     *
     * @param id 角色id
     * @return 菜单列表
     */
    public List<MenuVO> getMenu(Integer id) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        List<Menu> list = menuMapper.selectList(queryWrapper);
        List<MenuVO> listVO = new ArrayList<>();
        for (Menu menu : list) {
            MenuVO menuVO = new MenuVO();
            BeanUtils.copyProperties(menu, menuVO);
            listVO.add(menuVO);
        }
        return listVO;
    }

    /**
     * 更新
     *
     * @param userDTO UserDTO
     * @return true/false
     */
    public boolean updateUser(UserDTO userDTO) {
        User user = userMapper.selectById(userDTO.getUserId());
        if (user == null) {
            return false;
        } else {
            //先查询再更新，乐观锁生效
            BeanUtils.copyProperties(userDTO, user);
            //更新数据
            userMapper.updateById(user);
            return true;
        }
    }

    /**
     * 多条件+模糊查询
     *
     * @param userQueryVO userQueryVO
     * @return list
     */
    public List<UserDTO> findUser(UserQueryVO userQueryVO) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        Integer userId = userQueryVO.getUserId();
        String userCode = userQueryVO.getUserCode();
        String userName = userQueryVO.getUserName();
        String userGender = userQueryVO.getUserGender();
        String userRole = userQueryVO.getUserRole();
        String userState = userQueryVO.getUserState();
        if (userId != null) {
            wrapper.eq("user_id", userId);
        }
        if (!StringUtils.isEmpty(userCode)) {
            wrapper.eq("user_code", userCode);
        }
        if (!StringUtils.isEmpty(userName)) {
            wrapper.like("user_name", userName);
        }
        if (!StringUtils.isEmpty(userGender)) {
            wrapper.eq("user_gender", userGender);
        }
        if (!StringUtils.isEmpty(userRole)) {
            wrapper.eq("user_role", userRole);
        }
        if (!StringUtils.isEmpty(userState)) {
            wrapper.eq("user_state", userState);
        }
        List<User> list = this.list(wrapper);
        List<UserDTO> userDTOList = new ArrayList<>();
        for (User user : list) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(user, userDTO);
            userDTOList.add(userDTO);
        }
        return userDTOList;
    }

    /**
     * 分页
     *
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @return list
     */
    public List<UserDTO> getListDTO(Integer pageNum, Integer pageSize) {

        Page<User> pageInfo = new Page<>(pageNum, pageSize);
        this.page(pageInfo);
        List<User> list = pageInfo.getRecords();
        List<UserDTO> userDTOList = new ArrayList<>();
        for (User user : list) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(user, userDTO);
            userDTOList.add(userDTO);
        }
        return userDTOList;
    }
}
